package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.ldpf.YjSingle;
import com.app.domain.sxdh.SnResponseResult;
import com.app.domain.sxdh.SnYjDataVo;
import com.app.domain.sxdh.SnYjVo;
import com.app.domain.wxgd.WxgdData;
import com.app.domain.wxgd.WxgdResponseResult;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class SxdhService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public SnYjVo getData(YjData yjData, UploadParameter uploadParameter, String uuid, String equNum) {
        SnYjVo yjSingle = new SnYjVo();
        if (org.springframework.util.StringUtils.isEmpty(uuid)) {
            yjSingle.setUuId(UUID.randomUUID().toString().replace("-", ""));
        } else {
            yjSingle.setUuId(uuid);
        }
        yjSingle.setEquNum(equNum);
        yjSingle.setComponentId(yjSingle.getUuId());
        yjSingle.setEngineeringName(uploadParameter.getGcmc());
        yjSingle.setEngineeringSite(uploadParameter.getSgbw());
        yjSingle.setComponentParts(yjData.getBridge_code());
        yjSingle.setBeamType(uploadParameter.getLblx());
        yjSingle.setConcreteStrength(uploadParameter.getConcretestrength());
        yjSingle.setModulusOfElasticity(uploadParameter.getPumppressure());
        yjSingle.setBridgeName(uploadParameter.getEquipmentname());
        yjSingle.setTrialDate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
        yjSingle.setDosage(uploadParameter.getCjsjl());
        yjSingle.setMudjackDosage(uploadParameter.getYajiangji());
        yjSingle.setMudjackMode(uploadParameter.getYajiangmoshi());
        yjSingle.setMudjackType("大循环");


        List<SnYjDataVo> wxgdDataList = new ArrayList<>();
        SnYjDataVo wxgdData = new SnYjDataVo();
        wxgdData.setSteelBeamNo(yjData.getDuct_code());
        wxgdData.setSteelStrand(uploadParameter.getKongdaoshu());
        wxgdData.setPulpingOrientation(uploadParameter.getYajiangfangxiang());
        wxgdData.setPulpingOrder(uploadParameter.getYajiangbuzhou());
        wxgdData.setFlowVelocity(uploadParameter.getMsl());
        wxgdData.setFluidity(yjData.getPractical_fluidity());
        wxgdData.setMixProportion(uploadParameter.getPhb());
        wxgdData.setWaterRatio(String.valueOf(yjData.getWater_binder_ratio()));
        wxgdData.setStirringTime(uploadParameter.getStirtime());
        wxgdData.setStartDate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
        wxgdData.setEndDate(yjData.getMudjack_date() + " " + yjData.getEnd_time());
        wxgdData.setPulpingVolume(String.valueOf(yjData.getPractical_mudjack_value()));
        wxgdData.setStandardVolume(uploadParameter.getVolume());
        wxgdData.setPulpingPressureIn(String.valueOf(yjData.getMudjack_pressure()));
        wxgdData.setPulpingPressureOut(String.valueOf(yjData.getSlurry_pressure()));
        wxgdData.setHoldingTime(yjData.getPressurize_time());
        Map<String, List> graphData = yjDataService.getNewGraphData2(String.valueOf(yjData.getId()));
        List<String> mcgList = graphData.get("mcgList");
        List<String> mcgOutList = graphData.get("mcgOutList");
        List<Float> mcgVolumeInList = graphData.get("mcgVolumeInList");
        List<Float> mcgVolumeOutList = graphData.get("mcgVolumeOutList");
        List<String> mcgVolumeList = mcgVolumeInList.stream().map(Object::toString).collect(Collectors.toList());
        List<String> mcgVolumeOutList1 = mcgVolumeOutList.stream().map(Object::toString).collect(Collectors.toList());
        wxgdData.setVolumeCurve(String.join(",",mcgVolumeList));
        wxgdData.setPressureinCurve(String.join(",",mcgList));
        wxgdData.setPressureoutCurve(String.join(",",mcgOutList));
        wxgdData.setPressureoutCurve(String.join(",",mcgVolumeOutList1));
        List<String> dateList = graphData.get("dateList");
        wxgdData.setRecordTime(String.join(",",dateList));
        wxgdData.setResult("0");
        wxgdData.setUserId(uploadParameter.getBeiyong());
        wxgdData.setOtherInformation("/");
        wxgdData.setParentId(yjSingle.getUuId());
        wxgdData.setUuId(UUID.randomUUID().toString().replace("-", ""));
        wxgdData.setVacuumPumpPressure("0");
        wxgdData.setPulpingCount("1");
        wxgdData.setPulpingOutVolume("0");

//        wxgdData.setVolumeOutCurve("");
        wxgdDataList.add(wxgdData);
        yjSingle.setDataList(wxgdDataList);
        return yjSingle;
    }


    public SnResponseResult sendMessage(String serviceUrl, SnYjVo yjSingle, String token) {
        String requestJson = JSON.toJSONString(yjSingle);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        SnResponseResult sqResponseResult = null;
        try {
            String result = HttpClients.requestPost(serviceUrl, requestJson, map);
            System.out.println(result);
            sqResponseResult = (SnResponseResult) JSON.parseObject(StringUtil.replaceBlank(result), SnResponseResult.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sqResponseResult;
    }


}
